package com.test;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.hbase.HBaseUtility;

/**
 * 使用phoenix进行hbase表的增删改查
 * 
 * @author wb6214
 * 
 */
public class PhoenixDemo {

	public static void main(String[] args) throws SQLException {
		createTable();
		putData();
		queryData();
		delTable();
	}

	// 使用phoenix创建一张表
	public static void createTable() throws SQLException {
		String sql = "create table student(stuid integer not null primary key,name varchar,age integer,score integer,classid integer)";
		Connection conn = HBaseUtility.getConnection();
		Statement stmt = conn.createStatement();
		boolean n = stmt.execute(sql);
		conn.commit();
		System.out.println("创建表成功：" + sql);
		stmt.close();
		conn.close();
	}

	// 删除一张表
	public static void delTable() throws SQLException {
		String sql = "drop TABLE student";
		Connection conn = HBaseUtility.getConnection();
		Statement stmt = conn.createStatement();
		boolean n = stmt.execute(sql);
		conn.commit();
		System.out.println("删除表成功：" + sql);
		stmt.close();
		conn.close();
	}

	// 插入数据
	public static void putData() throws SQLException {
		String sql = "upsert into student(stuid,name,age,score,classid) values (1,'李磊',19,66,1)";
		Connection conn = HBaseUtility.getConnection();
		Statement stmt = conn.createStatement();
		boolean n = stmt.execute(sql);
		conn.commit();
		System.out.println("插入数据成功：" + sql);
		stmt.close();
		conn.close();
	}

	// 查询数据
	public static void queryData() throws SQLException {
		String sql = "select * from STUDENT";
		Connection conn = HBaseUtility.getConnection();
		Statement stmt = conn.createStatement();
		ResultSet result = stmt.executeQuery(sql);
		while (result != null && result.next()) {
			System.out.println("" + result.getString(1) + "           "
					+ result.getString(2) + "           " + result.getString(3)
					+ "           " + result.getString(4) + "           "
					+ result.getString(5));

		}
		result.close();
		stmt.close();
		conn.close();
	}
}
